﻿/********************************************************
 * Module Name    : 
 * Purpose        : Inventory Move Line Model
 * Class Used     : X_M_Movementline
 * Chronological Development
 * Veena         26-Oct-2009
 ******************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Data;
using VAdvantage.Classes;
using VAdvantage.Common;
using VAdvantage.Process;
using VAdvantage.DataBase;
using VAdvantage.Utility;
using VAdvantage.Logging;

namespace VAdvantage.Model
{
    /// <summary>
    /// Inventory Move Line Model
    /// </summary>
    public class MMovementLine : X_M_MovementLine
    {
        /** Parent							*/
        private MMovement _parent = null;

        /// <summary>
        /// Standard Constructor
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="M_MovementLine_ID">id</param>
        /// <param name="trxName">transaction</param>
	    public MMovementLine (Ctx ctx, int M_MovementLine_ID, String trxName)
            : base (ctx, M_MovementLine_ID, trxName)
	    {
		    if (M_MovementLine_ID == 0)
		    {
		    //	SetM_LocatorTo_ID (0);	// @M_LocatorTo_ID@
		    //	SetM_Locator_ID (0);	// @M_Locator_ID@
		    //	SetM_MovementLine_ID (0);			
		    //	SetLine (0);	
		    //	SetM_Product_ID (0);
			    SetM_AttributeSetInstance_ID(0);	//	ID
			    SetMovementQty (Env.ZERO);	// 1
			    SetTargetQty (Env.ZERO);	// 0
			    SetScrappedQty(Env.ZERO);
			    SetConfirmedQty(Env.ZERO);
			    SetProcessed (false);
		    }	
	    }

	    /// <summary>
        /// Load Constructor
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="dr">data row</param>
        /// <param name="trxName">transation</param>
        public MMovementLine(Ctx ctx, DataRow dr, String trxName)
            : base(ctx, dr, trxName)
        {
        }

	    /// <summary>
	    /// Parent constructor
	    /// </summary>
	    /// <param name="parent">parent</param>
	    public MMovementLine (MMovement parent)
            : this(parent.GetCtx(), 0, parent.Get_TrxName())
	    {
		    SetClientOrg(parent);
		    SetM_Movement_ID(parent.GetM_Movement_ID());
	    }
    	
	    /// <summary>
        /// Get AttributeSetInstance To
	    /// </summary>
	    /// <returns>asi</returns>
	    public int GetM_AttributeSetInstanceTo_ID ()
	    {
		    int M_AttributeSetInstanceTo_ID = base.GetM_AttributeSetInstanceTo_ID();
		    if (M_AttributeSetInstanceTo_ID == 0)
			    M_AttributeSetInstanceTo_ID = base.GetM_AttributeSetInstance_ID();
		    return M_AttributeSetInstanceTo_ID;
	    }
    	
	    /// <summary>
        /// Add to Description
	    /// </summary>
	    /// <param name="description">description text</param>
	    public void AddDescription (String description)
	    {
		    String desc = GetDescription();
		    if (desc == null)
			    SetDescription(description);
		    else
			    SetDescription(desc + " | " + description);
	    }

	    /// <summary>
	    /// Get Product
	    /// </summary>
        /// <returns>product or null if not defined</returns>
	    public MProduct GetProduct()
	    {
		    if (GetM_Product_ID() != 0)
			    return MProduct.Get(GetCtx(), GetM_Product_ID());
		    return null;
	    }
    	
	    /// <summary>
        /// Set Product - Callout
	    /// </summary>
	    /// <param name="oldM_Product_ID">old value</param>
	    /// <param name="newM_Product_ID">new value</param>
	    /// <param name="windowNo">window</param>
	    public void SetM_Product_ID (String oldM_Product_ID, String newM_Product_ID, int windowNo)
	    {
		    if (newM_Product_ID == null || newM_Product_ID.Length == 0)
			    return;
		    int M_Product_ID = int.Parse(newM_Product_ID);
		    if (M_Product_ID == 0)
			    return;
		    //
		    base.SetM_Product_ID(M_Product_ID);
		    if (GetCtx().GetContextAsInt(Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID") == M_Product_ID
			    && GetCtx().GetContextAsInt(Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID") != 0)
			    SetM_AttributeSetInstance_ID(GetCtx().GetContextAsInt(Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID"));
		    else
			    SetM_AttributeSetInstance_ID(0);
	    }
    	
    	
	    /// <summary>
        /// Set Movement Qty - enforce UOM
	    /// </summary>
	    /// <param name="movementQty">qty</param>
	    public void SetMovementQty (Decimal? movementQty)
	    {
		    if (movementQty != null)
		    {
			    MProduct product = GetProduct();
			    if (product != null)
			    {
				    int precision = product.GetUOMPrecision();
                    movementQty = Decimal.Round(movementQty.Value, precision, MidpointRounding.AwayFromZero);
			    }
		    }
		    base.SetMovementQty(movementQty);
	    }
    	   	
	    /// <summary>
	    /// Get Parent
	    /// </summary>
        /// 
        /// <returns>Parent Movement</returns>
	    public MMovement GetParent() 
	    {
		    if (_parent == null)
			    _parent = new MMovement (GetCtx(), GetM_Movement_ID(), Get_TrxName());
		    return _parent;
	    }

    	
	    /// <summary>
        /// Before Save
	    /// </summary>
	    /// <param name="newRecord">new</param>
	    /// <returns>true</returns>
	    protected override Boolean BeforeSave (Boolean newRecord)
	    {
		    //	Set Line No
		    if (GetLine() == 0)
		    {
                String sql = "SELECT COALESCE(MAX(Line),0)+10 AS DefaultValue FROM M_MovementLine WHERE M_Movement_ID=" + GetM_Movement_ID();
			    int ii = DataBase.DB.GetSQLValue (Get_TrxName(), sql);
			    SetLine (ii);
		    }
    		
		    if (GetM_Locator_ID() == GetM_LocatorTo_ID())
		    {
			    log.SaveError("Error", Msg.ParseTranslation(GetCtx(), "@M_Locator_ID@ == @M_LocatorTo_ID@"));
			    return false;
		    }

		    if (Env.Signum(GetMovementQty()) == 0)
		    {
			    log.SaveError("FillMandatory", Msg.GetElement(GetCtx(), "MovementQty"));
			    return false;
		    }

		    //	Qty Precision
		    if (newRecord || Is_ValueChanged("QtyEntered"))
			    SetMovementQty(GetMovementQty());

		    //	Mandatory Instance
		    if (GetM_AttributeSetInstanceTo_ID() == 0)
		    {
			    if (GetM_AttributeSetInstance_ID() != 0)	//	Set to from
				    SetM_AttributeSetInstanceTo_ID(GetM_AttributeSetInstance_ID());
			    else
			    {
				    MProduct product = GetProduct();
				    if (product != null
					    && product.GetM_AttributeSet_ID() != 0)
				    {
					    MAttributeSet mas = MAttributeSet.Get(GetCtx(), product.GetM_AttributeSet_ID());
					    if (mas.IsInstanceAttribute() 
						    && (mas.IsMandatory() || mas.IsMandatoryAlways()))
					    {
						    log.SaveError("FillMandatory", Msg.GetElement(GetCtx(), "M_AttributeSetInstanceTo_ID"));
						    return false;
					    }
				    }
			    }
		    }	//	ASI
    		
		    return true;
	    }
    }
}
